import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:qgchat/common/class/date/index.dart';
import 'package:qgchat/common/class/eventBus/index.dart';
import 'package:qgchat/common/global/global.dart';
import '../BloC/commentBloC.dart';
import 'package:qgchat/http/models/commentListModel.dart';
import 'package:qgchat/widgets/due/dif.dart';

class CommentList extends StatefulWidget {
  CommentList({Key key}) : super(key: key);

  @override
  _CommentListState createState() => _CommentListState();
}

class _CommentListState extends State<CommentList> {
  @override
  void initState() {
    super.initState();
    BlocProvider.of<CommentBloc>(context).add(CommentEvent.refresh);
  }

  @override
  Widget build(BuildContext context) {
    return BlocBuilder<CommentBloc, List<CommentListModel>>(
      builder: (context, state) {
        return Container(
          child: Column(
            children: state.map<Widget>((item) {
              return Container(
                margin: EdgeInsets.only(top: px(40)),
                child: FlatButton(
                  onPressed: () {
                    bus.emit("dtfocus", {
                      "pid": item.id,
                      "nickname": item.memberNickname,
                    });
                  },
                  padding: EdgeInsets.zero,
                  child: Row(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: <Widget>[
                      // 头像
                      ClipOval(
                        child: Container(
                          width: px(70),
                          height: px(70),
                          color: col("e4007f"),
                          child: CachedNetworkImage(
                            imageUrl: "${item.memberHead}",
                            fit: BoxFit.cover,
                            errorWidget: (context, url, error) {
                              return Config.errorNetImage;
                            },
                          ),
                        ),
                      ),
                      // 昵称 时间 评论 回复
                      Expanded(
                        child: Container(
                          margin: EdgeInsets.only(left: px(20)),
                          child: Column(
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: <Widget>[
                              // 昵称 时间
                              Row(
                                mainAxisAlignment:
                                    MainAxisAlignment.spaceBetween,
                                children: <Widget>[
                                  If(
                                    dif: item.parentName != null,
                                    ifView: RichText(
                                      text: TextSpan(
                                        style: TextStyle(
                                          color: col("303133"),
                                          fontSize: px(28),
                                        ),
                                        children: <TextSpan>[
                                          TextSpan(
                                            text: "${item.memberNickname}",
                                            style: TextStyle(
                                              color: col("3a488c"),
                                            ),
                                          ),
                                          TextSpan(text: " 回复 "),
                                          TextSpan(
                                            text: "${item.parentName}",
                                            style: TextStyle(
                                              color: col("3a488c"),
                                            ),
                                          ),
                                        ],
                                      ),
                                    ),
                                    elseView: RichText(
                                      text: TextSpan(
                                        style: TextStyle(
                                          color: col("3a488c"),
                                          fontSize: px(30),
                                        ),
                                        children: <TextSpan>[
                                          TextSpan(
                                            text: "${item.memberNickname}",
                                          ),
                                        ],
                                      ),
                                    ),
                                  ),
                                  Text(
                                    "${Date.simple(int.parse(item.createtime))}",
                                    style: TextStyle(
                                      color: col("606266"),
                                      fontSize: px(24),
                                    ),
                                  ),
                                ],
                              ),
                              // 评论
                              Container(
                                margin: EdgeInsets.only(top: px(15)),
                                child: Text(
                                  "${item.text}",
                                  style: TextStyle(
                                    color: col("303133"),
                                    fontSize: px(28),
                                  ),
                                ),
                              ),
                            ],
                          ),
                        ),
                      )
                    ],
                  ),
                ),
              );
            }).toList(),
          ),
        );
      },
    );
  }
}
